package com.haoshuang.zhouzhoubang.ui.page.shop.car

import android.os.Bundle
import android.os.CountDownTimer
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityShopCarBinding
import com.haoshuang.zhouzhoubang.model.bean.ShopBean5
import com.haoshuang.zhouzhoubang.ui.page.shop.confirm.OrderQrActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible
import java.text.DecimalFormat


/**
 *  购物车
 */
class ShopCarActivity : BaseVMActivity<ShopCarViewModel, ActivityShopCarBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "购物车"
    }

    var list = arrayListOf<ShopBean5>()
    lateinit var adapter: ShopCarAdapter
    override fun initData() {
        binding.apply {
            ivAll.clickWithTrigger {
                if (isAll == 0) {
                    isAll = 1
                    var price: Double = 0.0
                    for (b in list) {
                        b.isCheck = 1
                        price = b.price.toDouble() * b.cart_num + price
                    }
                    binding.ivAll.setImageResource(R.mipmap.dz_ok)
                    val df = DecimalFormat("#0.00")
                    var order_amount = df.format(price)
                    binding.tvPrice.text = "¥${order_amount}"
                } else {
                    for (b in list) {
                        b.isCheck = 0
                    }
                    isAll = 0
                    binding.ivAll.setImageResource(R.mipmap.dz_wx)
                    binding.tvPrice.text = "¥${0.00}"
                }
                adapter.notifyDataSetChanged()
            }
            tvDel.clickWithTrigger {
                var ids = ""
                for (b in list) {
                    if (b.isCheck == 1) {
                        ids = ids + b.id + ","
                    }
                }
                if (ids.isNullOrBlank()) {
                    toast("请选中在提交")
                    return@clickWithTrigger
                }
                binding.tvPrice.text = "¥${0.00}"
                vm.getData2("ids" to ids.substring(0, ids.length - 1))
            }
            tvQjs.clickWithTrigger {
                var ids = ""
                for (b in list) {
                    if (b.isCheck == 1) {
                        ids = ids + b.id + ","
                    }
                }
                if (ids.isNullOrBlank()) {
                    toast("请选中在提交")
                    return@clickWithTrigger
                }
                startPage<OrderQrActivity>(
                    "orderType" to 1,
                    "productId" to ids.substring(0, ids.length - 1)
                )
            }
            adapter = ShopCarAdapter(list)
            rvList.layoutManager = LinearLayoutManager(this@ShopCarActivity)
            rvList.adapter = adapter

            adapter.setOnItemChildClick(id = R.id.ivCheck) { view, position, item ->
                if (list[position].isCheck == 0) {
                    list[position].isCheck = 1
                } else {
                    list[position].isCheck = 0
                }
                setData()
                adapter.notifyDataSetChanged()
            }

            adapter.setOnItemChildClick(id = R.id.tvAdd) { view, position, item ->
                position90 = position
                if (list[position].cart_num == 1) {
                    toast("不能小于1")
                } else {
                    num90 = list[position].cart_num - 1
                    vm.getData6("cartId" to list[position].id, "cartNum" to num90)
                }
            }

            adapter.setOnItemChildClick(id = R.id.tvAdd2) { view, position, item ->
                position90 = position
                if (list[position].cart_num == list[position].trueStock) {
                    toast("库存不足")
                } else {
                    num90 = list[position].cart_num + 1
                    list[position].trueStock - 1
                    vm.getData6("cartId" to list[position].id, "cartNum" to num90)
                }
            }
        }
        vm.getData()

    }

    var num90 = 0
    var position90 = 0
    var isAll = 0
    private fun setData() {
        isAll = 1
        var price: Double = 0.0
        for (b in list) {
            if (b.isCheck == 0) {
                isAll = 0
            } else {
                price = b.price.toDouble() * b.cart_num + price
            }
        }
        val df = DecimalFormat("#0.00")
        var order_amount = df.format(price)
        binding.tvPrice.text = "¥${order_amount}"
        if (isAll == 1) {
            binding.ivAll.setImageResource(R.mipmap.dz_ok)
        } else {
            binding.ivAll.setImageResource(R.mipmap.dz_wx)
        }
    }

    override fun startObserve() {
        vm.getData6.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                //toast(msg)
            }
            onSuccess {
                /* object : CountDownTimer(2000, 1000) {
                     override   fun onTick(millisUntilFinished: Long) {

                     }

                    override fun onFinish() {
                        dismissLoading()
                     }
                 }.start()*/


                list[position90].cart_num = num90
                var price: Double = 0.0
                for (b in list) {
                    if (b.isCheck == 1)
                        price = b.price.toDouble() * b.cart_num + price
                }
                val df = DecimalFormat("#0.00")
                var order_amount = df.format(price)
                binding.tvPrice.text = "¥${order_amount}"
                adapter.notifyItemChanged(position90)
            }
        }
        vm.getData2.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                vm.getData()
                binding.ivAll.setImageResource(R.mipmap.dz_wx)
            }
        }
        vm.getData.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                list.clear()
                if (it?.size!! > 0) {

                    list.addAll(it)

                }
                if (list.size > 0) {
                    binding.rvList.visible()
                } else {
                    binding.rvList.gone()
                }

                adapter.notifyDataSetChanged()
            }
        }
    }
}